package work;
/*
5) 求一个3*3矩阵对角线元素之和
 */
public class Work05 {
    public static void main(String[] args) {
        int[][] array = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };

        int sum = matrix(array);

        System.out.println("3x3矩阵对角线元素之和为: " + sum);
    }
    static int matrix(int[][] array){
        int sum = 0;
        int n = array.length;

        for (int i = 0; i < 3; i++) {
            sum += array[i][i];
            sum += array[i][n-1-i];
        }
        return sum;
    }
}
